package com.shiyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shiyi.system.entity.SysMenuRole;
import com.shiyi.system.mapper.SysMenuRoleMapper;
import com.shiyi.system.service.SysMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shiyi
 * @since 2020-11-19
 */
@Service
@Transactional
public class SysMenuRoleServiceImpl extends ServiceImpl<SysMenuRoleMapper, SysMenuRole> implements SysMenuRoleService {



    @Override
    public List<Integer> queryMenuIdsByRoleIds(List<Integer> roleIds) {
        if (roleIds == null || roleIds.size() <= 0) {
            return new ArrayList<>();
        }
        QueryWrapper<SysMenuRole> wrapper = new QueryWrapper<>();
        wrapper
                .select("menu_id")
                .in("role_id", roleIds);
        return this.list(wrapper).stream().map(SysMenuRole::getMenuId).collect(Collectors.toList());
    }

    @Override
    public boolean saveMenuRole(Integer roleId, List<Integer> menuIds) {
        if (menuIds != null && menuIds.size() > 0) {
            return this.baseMapper.saveMenuRole(roleId, menuIds);
        }
        return false;
    }

    @Override
    public boolean deleteByRoleId(Integer roleId) {
        UpdateWrapper<SysMenuRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id", roleId);
        return this.remove(wrapper);
    }


}
